﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ProjectSem40701.Admin
{
    public partial class AnswerEdit : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variables here
        /// </summary>
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter dpAnswer =
                    new AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter();
                int asid = Convert.ToInt32(Request.QueryString["asid"].ToString());
                DataTable dtttblAnswer =
                    dpAnswer.Get_tbl_Answer_Select_All_By_AnswerID(asid);
                this.txtAnswerTitle.Text = dtttblAnswer.Rows[0]["AnswerTitle"].ToString();
                this.txtAnswerContent.Value = Server.HtmlDecode(dtttblAnswer.Rows[0]["AnswerContent"].ToString());
                this.chkbCorrect.Checked = Convert.ToBoolean(dtttblAnswer.Rows[0]["AnswerCorrect"].ToString());
                
                foreach (ListItem item in this.cbbAnswerStatus.Items)
                {
                    if (item.Value == dtttblAnswer.Rows[0]["AnswerStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }
                Session["qid"] = dtttblAnswer.Rows[0]["QuestionID"].ToString();

            }
            catch
            { 
                //bao loi
            }
        }

        /// <summary>
        /// Khi người dùng ấn nút Click
        /// </summary>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter dpAnswer =
                    new AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter();
                int ok = dpAnswer.tbl_Answer_Update(
                    Convert.ToInt32(Session["qid"].ToString()),
                    Server.HtmlEncode(this.txtAnswerContent.Value.Trim()), 
                    this.chkbCorrect.Checked,
                    this.txtAnswerTitle.Text.Trim(),
                    Convert.ToInt32(this.cbbAnswerStatus.SelectedValue.ToString()),
                    Convert.ToInt32(Request.QueryString["asid"].ToString()));
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else 
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            { 
                //bao loi
                Show_Dialog(FAIL);
            }
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter dpAnswer =
                    new AdminXSD.AnswerTableAdapters.tbl_Answer_Select_AllTableAdapter();
                int ok = dpAnswer.tbl_Answer_Delete(Convert.ToInt32(Request.QueryString["asid"]));
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog_Del(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            {
                //bao loi
                Show_Dialog_Del(FAIL);
            }
        }

        /// <summary>
        /// Đóng modal popup
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("QuestionEdit.aspx?qid=" + Request.QueryString["qid"].ToString());
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// show dialog
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// show dialog neu an xoa
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
